package com.ruoyi.web.controller.fx;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.dto.FxGoodPurchaseDto;
import com.ruoyi.system.domain.fx.FxGoodPurchase;
import com.ruoyi.system.domain.fx.FxSupplierGood;
import com.ruoyi.system.service.fx.IFxGoodPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品采购Controller
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/fx/purchase")
public class FxGoodPurchaseController extends BaseController {
    @Autowired
    private IFxGoodPurchaseService fxGoodPurchaseService;

    /**
     * 查询商品采购列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FxGoodPurchase fxGoodPurchase) {
        startPage();
        List<FxGoodPurchase> list = fxGoodPurchaseService.selectFxGoodPurchaseList(fxGoodPurchase);
        return getDataTable(list);
    }


    @GetMapping("/getCount")
    public TableDataInfo getCount(FxGoodPurchase fxGoodPurchase) {
        //startPage();
        List<FxGoodPurchase> list = fxGoodPurchaseService.selectFxGoodPurchaseList(fxGoodPurchase);
        return getDataTable(list);
    }

    /**
     * 导出商品采购列表
     */
    @Log(title = "商品采购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxGoodPurchase fxGoodPurchase) {
        List<FxGoodPurchase> list = fxGoodPurchaseService.selectFxGoodPurchaseList(fxGoodPurchase);
        ExcelUtil<FxGoodPurchase> util = new ExcelUtil<FxGoodPurchase>(FxGoodPurchase.class);
        util.exportExcel(response, list, "商品采购数据");
    }

    /**
     * 获取商品采购详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxGoodPurchaseService.selectFxGoodPurchaseById(id));
    }

    /**
     * 新增商品采购
     */
    @Log(title = "商品采购", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxGoodPurchase fxGoodPurchase) {
        return toAjax(fxGoodPurchaseService.insertFxGoodPurchase(fxGoodPurchase));
    }

    /**
     * 确定采购
     */
//    @Log(title = "采购", businessType = BusinessType.INSERT)
//    @PostMapping("/addPurchase")
//    public AjaxResult addPurchase(@RequestBody FxGoodPurchaseDto fxGoodPurchaseDto) {
//        return toAjax(fxGoodPurchaseService.insertPurchase(fxGoodPurchaseDto));
//    }

    /**
     * 批量采购
     *
     * @param fxGoodPurchaseDto
     * @return
     */
    @Log(title = "批量采购", businessType = BusinessType.INSERT)
    @PostMapping("/adds")
    public AjaxResult adds(@RequestBody FxGoodPurchaseDto fxGoodPurchaseDto) {
        fxGoodPurchaseDto.setCreateUser(getUserId());
        return toAjax(fxGoodPurchaseService.insertMultiGoodPurchase(fxGoodPurchaseDto));
    }

    /**
     * 修改商品采购
     */
    @Log(title = "商品采购", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxGoodPurchase fxGoodPurchase) {
        return toAjax(fxGoodPurchaseService.updateFxGoodPurchase(fxGoodPurchase));
    }

    /**
     * 删除商品采购
     */
    @Log(title = "商品采购", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fxGoodPurchaseService.deleteFxGoodPurchaseByIds(ids));
    }
}
